package com.cade.agriculturemall.widget.wheel.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import com.addresspicker.huichao.addresspickerlibrary.wheel.OnWheelChangedListener;
import com.addresspicker.huichao.addresspickerlibrary.wheel.OnWheelClickedListener;
import com.addresspicker.huichao.addresspickerlibrary.wheel.WheelView;
import com.addresspicker.huichao.addresspickerlibrary.wheel.adapter.AbstractWheelTextAdapter;
import com.cade.agriculturemall.R;
import com.cade.agriculturemall.bean.StreetChildsBean;

import java.util.ArrayList;
import java.util.List;


public class StressPickerDialog extends Dialog {

    private final static int DEFAULT_ITEMS = 5;
    private final static int UPDATE_CITY_WHEEL = 11;
    private final static int UPDATE_COUNTY_WHEEL = 12;
    AbstractWheelTextAdapter StressAdapter;
    private Activity mContext;

    private List<StreetChildsBean> mStreetes = new ArrayList<StreetChildsBean>();
    private WheelView stressWheel;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (!isShowing()) {
                return;
            }

        }
    };

    public StressPickerDialog(Activity context, List<StreetChildsBean> streetes, StreetChildsBean defaultStreet,
                              final onCityPickedListener listener) {
        super(context);
        mContext = context;
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getWindow().setGravity(Gravity.BOTTOM);
        getWindow().setBackgroundDrawable(
                new ColorDrawable(Color.parseColor("#00000000")));
        getWindow().setWindowAnimations(com.addresspicker.huichao.addresspickerlibrary.R.style.AnimBottom);
        View rootView = getLayoutInflater().inflate(R.layout.dialog_stress_picker, null);
        int screenWidth = mContext.getWindowManager().getDefaultDisplay()
                .getWidth();
        LayoutParams params = new LayoutParams(screenWidth,
                LayoutParams.MATCH_PARENT);
        super.setContentView(rootView, params);

        mStreetes.addAll(streetes);

        initViews();
        setDefaultArea(defaultStreet);

        View done = findViewById(com.addresspicker.huichao.addresspickerlibrary.R.id.done);
        done.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (listener != null) {
                    StreetChildsBean province = mStreetes.size() > 0 ? mStreetes
                            .get(stressWheel.getCurrentItem()) : null;
                    listener.onPicked(province);
                }
                dismiss();
            }
        });

        View cancel = findViewById(com.addresspicker.huichao.addresspickerlibrary.R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }

    private void setDefaultArea(StreetChildsBean defaultStreet) {

        int StressItem = 0;

        if (defaultStreet == null) {
            defaultStreet = mStreetes.get(0);
            StressItem = 0;
        } else {
            for (int i = 0; i < mStreetes.size(); i++) {
                if (mStreetes.get(i).getCode()
                        .equals(defaultStreet.getCode())) {
                    StressItem = i;
                    break;
                }
            }
        }

        stressWheel.setCurrentItem(StressItem, false);

    }

    private void initViews() {
        stressWheel = (WheelView) findViewById(R.id.dialog_viewstressWheel);
        StressAdapter = new AbstractWheelTextAdapter(mContext,
                com.addresspicker.huichao.addresspickerlibrary.R.layout.wheel_text) {
            @Override
            public int getItemsCount() {
                return StressPickerDialog.this.mStreetes.size();
            }

            @Override
            protected CharSequence getItemText(int index) {

                return StressPickerDialog.this.mStreetes.get(index).getName();
            }
        };


        stressWheel.setViewAdapter(StressAdapter);
        stressWheel.setCyclic(false);
        stressWheel.setVisibleItems(DEFAULT_ITEMS);

        OnWheelClickedListener clickListener = new OnWheelClickedListener() {

            @Override
            public void onItemClicked(WheelView wheel, int itemIndex) {
                if (itemIndex != wheel.getCurrentItem()) {
                    wheel.setCurrentItem(itemIndex, true, 500);
                }
            }
        };

        stressWheel.addClickingListener(clickListener);

        stressWheel.addChangingListener(new OnWheelChangedListener() {

            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                mHandler.removeMessages(UPDATE_CITY_WHEEL);
                Message msg = Message.obtain();
                msg.what = UPDATE_CITY_WHEEL;
                msg.arg1 = newValue;
                mHandler.sendMessageDelayed(msg, 50);
            }
        });


    }

    public interface onCityPickedListener {
        void onPicked(StreetChildsBean selectStreet);
    }
}
